/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 조회 - Proxy
 * 				공지사항 조회하는 Model Logic
 */
package com.skt.common_hr.congcon.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.congcon.CongconFacade;
	import com.skt.common_hr.congcon.model.vo.NoticeVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;

	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class NoticeViewProxy extends CommonProxy
	{
		public static const NAME:String = "NoticeProxy";

		public var noticeList:ArrayCollection;
		public var rowCount:int;
		//public var vo:NoticeVO;


		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function NoticeViewProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function loadList(pastData:Boolean):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"] = "04";
			if(pastData) {
				param["I_MODE"]		= "M"; //관리자
			}
			else {
				param["I_MODE"]		= "U"; //일반사용자
			}
			loadData("Z_HR_0031", param, result1, fault1);
		}

		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param target: 1:제목,2:작성자이름,3:내용
		 * @return void
		 */
		public function keywordSearch(target:String, keyWord:String):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]	= "04";
			param["I_RA_SEARCH"] = target;
			param["I_SEARCH"]	= keyWord;
			param["I_MODE"]		= "M"; //관리자

			loadData("Z_HR_0032", param, result1, fault1);
		}


		//삭제
		public function deleteData(paramVo:Object):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]		= paramVo.I_BOARD_ID;
			param["I_CONTENT_IDX"]		= paramVo.CONTENT_IDX;

			loadData("Z_HR_0039", param, updateResult, updateFault);
		}

		public function increaseCount(index:Number):void
		{
			var param:Object = new Object();
			param["I_BOARD_ID"]		= "04";
			param["I_CONTENT_IDX"]		= index.toString();
			trace(index);
			loadData("Z_HR_0034", param, countResult, countFault);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result1(event:ResultEvent):void
		{
			if(retCode==0) {
				noticeList = parseNotice(event.result.TBL_BOARD);
				facade.sendNotification(CongconFacade.LOAD_NOTICE_COMPLETE, this);
			}
			else {
				facade.sendNotification(CongconFacade.LOAD_NOTICE_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault1(event:FaultEvent):void
		{
			facade.sendNotification(CongconFacade.LOAD_NOTICE_FAILED, this);
		}


		public function updateResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(CongconFacade.UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				facade.sendNotification(CongconFacade.UPDATE_NOTICE_COMPLETE, this);
			}
		}


		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(CongconFacade.UPDATE_NOTICE_FAILED, this);
		}

		public function countResult(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(CongconFacade.UPDATE_COUNT_COMPLETE, this);
			}
			else {
				facade.sendNotification(CongconFacade.UPDATE_COUNT_FAILED, this);
			}
		}


		private function countFault(event:FaultEvent):void
		{
			facade.sendNotification(CongconFacade.UPDATE_COUNT_FAILED, this);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 공지사항 Value Object
		 */
		 private function parseNotice(src:ArrayCollection):ArrayCollection
		{
			var noticeList:ArrayCollection = new ArrayCollection;

			for(var i:int = 0; i < src.length; i++)
			{
				var vo:NoticeVO = new NoticeVO();

				vo.SEQ		= src[i].SEQ;			//게시판 유형(번호범위 일련번호)


				vo.BOARD_ID		= src[i].BOARD_ID;			//게시판 유형(번호범위 일련번호)
				vo.CONTENT_IDX	= src[i].CONTENT_IDX;		//게시물 일련번호
				vo.TITLE		= src[i].TITLE;				//제목
				vo.CONTENT		= src[i].CONTENT;			//내용
				vo.BEGDA		= DateUtil.stringToDateTime(src[i].BEGDA);				//시작일
				vo.ENDDA		= DateUtil.stringToDateTime(src[i].ENDDA);				//종료일
				vo.CREATE_DATE	= DateUtil.stringToDateTime(src[i].CREATE_DATE);		//게시일자(수정일자)
				vo.WRITER_NAME	= src[i].WRITER_NAME;		//사원 또는 지원자의 포맷된 이름
				vo.READNUM		= src[i].READNUM;			//조회수
				vo.FILE_NAME	= src[i].FILE_NAME;			//파일이름
				vo.FILE_URL		= src[i].FILE_URL;
				trace("filename = " + vo.FILE_NAME);
				vo.bAttatched = (vo.FILE_NAME!=null) ? true : false;
				noticeList.addItem(vo);
			}
			rowCount = (i==0)?1:i;

			return noticeList;
		}
	}
}